<?php 
if (!isset($_SESSION)) {
  session_start();
}
define("CCTRL", "category");
define("CCTRLNAME", "category");
define("TOKEN", "12dfdbc68f87c1bf1a131328911e0968");
include APPPATH . 'libraries/defu.php';
class category extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       protected $configs;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->model('backend/type_model');
            $this->type_model=new type_model();
            $this->load->model('colmod/category_colmod');
            $this->colModel=new category_colmod();
            $this->load->library('smarty3');
            $this->smarty=new CI_Smarty3();
            $this->smarty->error_reporting = E_ALL & ~E_NOTICE;
            if(!isset($_SESSION["ADP"][CCTRL]["display"]))$_SESSION["ADP"][CCTRL]["display"]=0;
        }
        function index(){
            if(!isset($_SESSION["ADP"]["USER"])){
                $this->smarty->display("backend/00_login"); 
                return;
            }
            if(!checkAuthority(CCTRLNAME)){
                $this->smarty
                        ->assign('_SESSION', $_SESSION)
                        ->display("backend/00_accessdenied");
                return;
            }
            //unset($_SESSION["ADP"][CCTRL."-JQX"]["colModel"]);
            if(isset($_SESSION["ADP"][CCTRL."-JQX"]["colModel"]))
                $colModel=$_SESSION["ADP"][CCTRL."-JQX"]["colModel"];
            else{
                $colModel=$this->colModel->get();
                $_SESSION["ADP"][CCTRL."-JQX"]["colModel"]=$colModel;
            }
            $Data["colModel"]=$colModel;
            $Data["display"]=$_SESSION["ADP"][CCTRL]["display"];
            
            $this->smarty
                    ->assign('Data', $Data)
                    ->assign('_SESSION', $_SESSION)
                    ->display("backend/category/category");
        }
        function gridscript(){
            $colModel=$this->colModel->get();
            header("Content-type:text/javascript");
            $this->smarty
                    ->assign("colModel", $colModel)
                    ->assign("isPopup", true)
                    ->display("backend/00_base/grid_advanced");
        }
        function script(){
            header("Content-type:text/javascript");
            $this->smarty->display("backend/".CCTRL."/handleEvent");
        }
        function edit($_id=""){
            $Data=null;
            if($_id!=""){
                $tmp=$this->type_model->get($_id);
                if(count($tmp)>0)$Data["o"]=$tmp[0];
            }
            
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('o', $Data["o"])
                    ->display("backend/".CCTRL."/02_edit");
        }
        function save(){
            $_id    =$this->input->post("_id");
            $_name  =$this->input->post("_name");
            $_alias =$this->input->post("_alias");
            $_value =$this->input->post("_value");
            $_des   =$this->input->post("_des");
            $messages=array();
            if($_name===false || $_name == ""){
                $messages[]    =   "[Shound be non-empty] Name shound be non-empty.";
            }
            if($_alias===false || convertUrl($_alias) == ""){
                $messages[]    =   "[Shound be non-empty] Alias shound be non-empty.";
            }
            if(count($messages)>0){
                $result   =   -1;
                $message    =   implode("<br/>", $messages);
                goto result;
            }
            $params=array(
                "_name"         =>  $_name,
                "_alias"        =>  convertUrl($_alias),
                "_value"        =>  $_value,
                "_description"  =>  $_des,
                "_type"         =>  "category"
            );
            if($_id==false){
                $params["_id"]  =   md5(time().rand());
                $rs=$this->type_model->insert($params);
            }else{
                $rs=$this->type_model->update($_id,$params);
            }
            if($rs===false){
                $result   =   -888;
                $message    =   "[Fail] Please check your data input and try again.<br/><br/>
                    Find out more about an <span class='csp'><tt onclick='_whyerror()'>Error Message</tt></span>
                    ";
            }else{
                $alog=array(
                    "_table"=>"Product",
                    "_row"=>null,
                    "_commandtype"=>$_id==false?"insert":"update",
                    "_log"=>print_r(array(
                        "user" =>$_SESSION["ADP"]["USER"]["Name"],
                        "ip"=>  getIP(),
                        "command"=>$_id==false?"insert":"update",
                        "id"    =>$_id
                    ),true)
                );
                @$this->backend_model->tolog($alog);
                $result   =   1;
                $message    =   "[Success]. Thành công.";
            }
            result:
            echo json_encode(array("result"=>$result,"message"=>$message));
        }
        function binding(){
            $data_rows=array();
            $result=$this->type_model->binding_category();
            $data[] = array(
                'TotalRows' => $result['total_rows'],
                'Rows' => $result['rows']
            );
            echo json_encode($data);
        }
}
?>
